/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.util.memcached.MemcachedKey;
/*     */ import com.zimbra.common.util.memcached.MemcachedMap;
/*     */ import com.zimbra.common.util.memcached.MemcachedSerializer;
/*     */ import com.zimbra.cs.memcached.MemcachedConnector;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ObjectInputStream;
/*     */ import java.io.ObjectOutputStream;
/*     */ import org.apache.commons.io.output.ByteArrayOutputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class MemcachedImapCache
/*     */   implements ImapSessionManager.Cache
/*     */ {
/*     */   private MemcachedMap<ImapMemcachedKey, ImapFolder> map;
/*     */   
/*     */   MemcachedImapCache()
/*     */   {
/*  41 */     this.map = new MemcachedMap(MemcachedConnector.getClient(), new ImapMemcachedSerializer());
/*     */   }
/*     */   
/*     */   public void put(String key, ImapFolder value)
/*     */   {
/*     */     try {
/*  47 */       synchronized (value) {
/*  48 */         this.map.put(new ImapMemcachedKey(key), value);
/*     */       }
/*     */     } catch (ServiceException e) {
/*  51 */       ZimbraLog.imap.warn("Failed to store into cache", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public ImapFolder get(String key)
/*     */   {
/*     */     try {
/*  58 */       return (ImapFolder)this.map.get(new ImapMemcachedKey(key));
/*     */     } catch (ServiceException e) {
/*  60 */       ZimbraLog.imap.warn("Failed to load from cache", e); }
/*  61 */     return null;
/*     */   }
/*     */   
/*     */   public void remove(String key)
/*     */   {
/*     */     try
/*     */     {
/*  68 */       this.map.remove(new ImapMemcachedKey(key));
/*     */     } catch (ServiceException e) {
/*  70 */       ZimbraLog.imap.warn("Failed to remove from cache", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void updateAccessTime(String key) {}
/*     */   
/*     */   private static final class ImapMemcachedKey implements MemcachedKey
/*     */   {
/*     */     private final String key;
/*     */     
/*     */     ImapMemcachedKey(String key)
/*     */     {
/*  82 */       this.key = key;
/*     */     }
/*     */     
/*     */     public String getKeyPrefix()
/*     */     {
/*  87 */       return "zmImap:";
/*     */     }
/*     */     
/*     */     public String getKeyValue()
/*     */     {
/*  92 */       return this.key;
/*     */     }
/*     */   }
/*     */   
/*     */   private static final class ImapMemcachedSerializer
/*     */     implements MemcachedSerializer<ImapFolder>
/*     */   {
/*     */     public Object serialize(ImapFolder folder) throws ServiceException
/*     */     {
/* 101 */       ByteArrayOutputStream bout = new ByteArrayOutputStream();
/* 102 */       ObjectOutputStream oout = null;
/*     */       try {
/* 104 */         oout = new ObjectOutputStream(bout);
/* 105 */         oout.writeObject(folder);
/*     */       } catch (Exception e) {
/* 107 */         throw ServiceException.FAILURE("Failed to serialize ImapFolder", e);
/*     */       } finally {
/* 109 */         Closeables.closeQuietly(oout);
/*     */       }
/* 111 */       return bout.toByteArray();
/*     */     }
/*     */     
/*     */     public ImapFolder deserialize(Object obj) throws ServiceException
/*     */     {
/* 116 */       ObjectInputStream in = null;
/*     */       try {
/* 118 */         in = new ObjectInputStream(new ByteArrayInputStream((byte[])obj));
/* 119 */         return (ImapFolder)in.readObject();
/*     */       } catch (Exception e) {
/* 121 */         throw ServiceException.FAILURE("Failed to deserialize ImapFolder", e);
/*     */       } finally {
/* 123 */         Closeables.closeQuietly(in);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/MemcachedImapCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */